home *** CD-ROM | disk | FTP | other *** search
- Path: engnews1.Eng.Sun.COM!taumet!clamage
- From: fjh@munta.cs.mu.OZ.AU (Fergus Henderson)
- Newsgroups: comp.std.c++
- Subject: Re: new T[0] and sizeof(T)
- Date: 2 Feb 1996 15:49:05 GMT
- Organization: Computer Science, University of Melbourne, Australia
- Sender: news@cs.mu.OZ.AU (CS-Usenet)
- Approved: clamage@eng.sun.com (comp.std.c++)
- Message-ID: <9603318.21879@mulga.cs.mu.OZ.AU>
- References: <1996Feb1.091641.4676@iiasa.ac.at>
- NNTP-Posting-Host: taumet.eng.sun.com
- Originator: clamage@taumet
-
- marek@iiasa.ac.at (Marek MAKOWSKI) writes:
-
- >I would like to ask for comments on three easy questions illustrated
- >by the following piece of code:
- >
- >template <class I, class T>
- >void mVect<I,T>::resize(I new_size) {
- > T *old = v;
- > v = new T[new_size]; // T *v is a private member of mVect
- > int size_of_elem = sizeof(T); // <--- question 3
- > //
- > // do something
- > //
- > delete[] old; // <--- questions 1 & 2
- >}
- >
- >I have the following questions:
- >1. Is it absolutely robust and portable to delete[] old, even
- > if a previous call was for new_size == 0 (or if v was allocated
- > by the ctor for the size == 0) ?
-
- The code
-
- T *old = new T[0];
- delete [] old;
-
- is definitely perfectly legal (strictly conforming).
-
- Whether or not it is in practice portable is another question -- that
- is something that can only be determined empirically.
-
- > In other words: is it guaranteed that:
- > (v = new T[0]) == 0;
-
- No, after 'v = new T[0]', it is guaranteed that `v' is *not* a null pointer.
-
- >2. Is it correct to assume that no destructor is called by this statement
- > if old was set as: old = new T[0] ??
-
- Yes.
-
- >3. Is there any risk involved in using sizeof(T) in this statement ?
-
- Basically no, but of course that would depend on what you used it for.
-
- >The Watcom code blows-up on the delete[] statement (if a previous
- >call was for new_size == 0)
-
- If what you have described is correct, then it sounds like a Watcom bug to me.
- (However, I suspect there is a good chance that there is a bug somewhere
- in the parts of your code that you haven't shown us.)
-
- >and gives a warning whenever it sees the sizeof(T).
-
- Compilers are allowed to warn about anything they like;
- however I don't see why warning about `sizeof(T)' would be useful.
-
- >If the answer for quaestions 1 and 2 is negative then I would like to
- >know the reason why the standard does not require new to return 0
- >for zero_size array of objects.
-
- Well, an array of size zero is conceptually different to no array at all.
-
- >If there is a good reason for allowing new to return "anything" in such
- >situations then one should add to every ctor setting a ptr to 0
- >(which indeed made the Watcom version of my application running).
-
- --
- Fergus Henderson WWW: http://www.cs.mu.oz.au/~fjh
- fjh@cs.mu.oz.au PGP: finger fjh@128.250.37.3
-
- [ comp.std.c++ is moderated. Submission address: std-c++@ncar.ucar.edu.
- Contact address: std-c++-request@ncar.ucar.edu. The moderation policy is
- summarized in http://reality.sgi.com/employees/austern_mti/std-c++/policy.html
- ]
-